from rest_framework import serializers

from .models import BookInfo

# class BookInfoModelSerializer(serializers.ModelSerializer):
#     """图书数据序列化器"""
#     class Meta:
#         model = BookInfo
#         fields = '__all__'


class HeroInfoSerializer(serializers.Serializer):
    """英雄数据序列化器"""
    GENDER_CHOICES = (
        (0, 'female'),
        (1, 'male')
    )
    id = serializers.IntegerField(label='ID', read_only=True)
    hname = serializers.CharField(label='名字', max_length=20)
    hgender = serializers.ChoiceField(choices=GENDER_CHOICES, label='性别', required=False)
    hcomment = serializers.CharField(label='描述信息', max_length=200, required=False, allow_null=True)
    # hbook = serializers.PrimaryKeyRelatedField(label='图书', read_only=True)
    # hbook = serializers.PrimaryKeyRelatedField(label='图书', queryset=BookInfo.objects.all())
    # hbook = serializers.StringRelatedField(label='图书', read_only=True)
    # hbook = BookInfoSerializer(read_only=True)



class BookInfoSerializer(serializers.Serializer):
    """图书数据序列化器"""
    id = serializers.IntegerField(label='ID', read_only=True)
    btitle = serializers.CharField(label='名称', max_length=20)
    bpub_date = serializers.DateField(label='发布日期', required=True)
    bread = serializers.IntegerField(label='阅读量', required=False)
    bcomment = serializers.IntegerField(label='评论量', required=False)
    image = serializers.ImageField(label='图片', required=False)
    # heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    heroinfo_set = HeroInfoSerializer(many=True, read_only=True)

    def validate_btitle(self, value):
        """对单个字段追加校验逻辑"""
        if 'django' not in value.lower():
            raise serializers.ValidationError('书名中不包含django')
        return value

    def validate(self, attrs):
        """同时对多个字段进行追加校验逻辑"""
        bread = attrs['bread']
        bcomment = attrs['bcomment']
        if bread < bcomment:
            raise serializers.ValidationError('阅读量小于评论量')
        return attrs

    def create(self, validated_data):
        """
                如果创建序列化器时只给data参数传递实参,那么将来调用序列化器的save方法时,就会调用此方法
                :param validated_data: 反序列化校验后的字典数据
                :return: 把新增模型对象返回
                """
        return BookInfo.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
               如果创建序列化器时,也给instance参数传入实参后,在调用序列化器的save方法时,就会调用此方法
               :param instance: 本次要进行修改的模型对象
               :param validated_data: 反序列化校验后的字典数据
               :return: 把修改后的模型对象返回
               """

        instance.btitle = validated_data.get('btitle')

        instance.bpub_date = validated_data.get('bpub_date')
        instance.save()

        return instance

class BookInfoModelSerializer(serializers.ModelSerializer):
    # book模型序列化器
    class Meta:
        model = BookInfo
        # fileds = '__all__' #把BookInfo模型中的字段全部映射过来
        # fields = ['id', 'btitle', 'bpub_date', 'abc']  # 生成指定的字段
        exclude = ['inage']
        # 如果对ModelSerializer自动生成的字段选项不满意,可以在下面这个字典中,修改字段选项
        extra_kwargs = {
            'bread': {'min_value': 0},
            'bcomment': {'min_value': 0},
            'is_delete': {'write_only': True}
        }

        read_only_fields = ['bread', 'bcomment']  # 修改那些字段为只做序列化输出
